package com.ztf.shopping.dao.imp;

import com.ztf.shopping.bean.Article;
import com.ztf.shopping.service.ArticleService;
import com.ztf.shopping.untils.JDBCUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ArticleDaoImp implements ArticleService {
    @Override
    public List<Article> findAll() {
        Connection connection = null;
        PreparedStatement prep = null;
        ResultSet resultSet = null;
        List<Article> list = new ArrayList<Article>();
        String sql = "select * from article";

        try {
            connection = JDBCUtils.getConnection();
            prep = connection.prepareStatement(sql);
            resultSet = prep.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String price = resultSet.getString(3);
                String pic = resultSet.getString(4);
                int number = resultSet.getInt(5);
                list.add(new Article(id,name,new BigDecimal(price),pic,number));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(resultSet,prep,connection);
        }

        return list;
    }
}
